/*
 *  Copyright (C) 2017 Bilibili
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *          http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.yunkaweilai.android.function.photopick;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.bilibili.boxing.AbsBoxingActivity;
import com.bilibili.boxing.AbsBoxingViewFragment;
import com.bilibili.boxing.model.entity.BaseMedia;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.utils.StatusBarUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 图片选择
 * A simple subclass of {@link AbsBoxingActivity}. Holding a {@link AbsBoxingViewFragment} to display medias.
 */
public class PhotoPickerActivity extends AbsBoxingActivity {

    @BindView(R.id.ic_img_back)
    ImageView mIcImgBack;
    @BindView(R.id.id_txt_pick_album)
    TextView mIdTxtPickAlbum;
    @BindView(R.id.content_layout)
    FrameLayout mContentLayout;
    private PhotoPickerFragment mPickerFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo_picker);
        StatusBarUtils.setWindowStatusBarColor(this,R.color.wjx_title_bg_next);
        ButterKnife.bind(this);
        mPickerFragment.setTitleTxt(mIdTxtPickAlbum);
    }

    @NonNull
    @Override
    public AbsBoxingViewFragment onCreateBoxingView(ArrayList<BaseMedia> medias) {
        mPickerFragment = (PhotoPickerFragment) getSupportFragmentManager().findFragmentByTag(PhotoPickerFragment.TAG);
        if (mPickerFragment == null) {
            mPickerFragment = (PhotoPickerFragment) PhotoPickerFragment.newInstance().setSelectedBundle(medias);
            getSupportFragmentManager().beginTransaction().replace(R.id.content_layout, mPickerFragment, PhotoPickerFragment.TAG).commit();
        }
        return mPickerFragment;
    }

    /**
     * 返回所选的图片
     *
     * @param intent
     * @param list
     */
    @Override
    public void onBoxingFinish(Intent intent, @Nullable List<BaseMedia> list) {
        setResult(Activity.RESULT_OK, intent);
        finish();
    }

    @OnClick(R.id.ic_img_back)
    public void onClick(View view) {
        onBackPressed();
    }

}
